import 'dart:async';

import 'package:path/path.dart' as p;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Lao si ji vido player',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Lao si ji vido player'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  String text = 'test';
  static const videoChannel = const MethodChannel(
      'com.njiandan.laosijivideo/video');


  Future _scanQrcode() async {
//    RegExp url = new RegExp(r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+");
//
//    String barcode = 'http://192.168.0.1/test/test.mp4?from=lanvideo';
//    _showSnackBar(url.hasMatch(barcode).toString() + ',' + p.extension(barcode).split(r'?')[0]);
//    return;
//    _showSnackBar('?????');
//    print('*****************************************************');
//    return '';
//    String barcode = await videoChannel.invokeMethod('scan');
//    setState(() => this.text = barcode);
//    _openWebviewVideo('', '', '');
//    print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
//    _showSnackBar(barcode);
//    return;
    try {
      String barcode = await videoChannel.invokeMethod('scan');
      RegExp url = new RegExp(r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+");
      if (url.hasMatch(barcode)) {
        List<String> params = p.extension(barcode).split(r'?');
        String ext = params[0];
        String param = '';
        if (params.length == 2) {
          param = params[1];
        }
        
      } else {
        _showSnackBar('不是有效的播放地址链接!');
      }
    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_NOT_GRANTED') {
        _showSnackBar('权限获取失败!');
      } else {
        _showSnackBar('Unknown error: $e');
      }
    } on FormatException {
      _showSnackBar('null (User returned using the "back"-button before scanning anything. Result)');
    } catch (e) {
      _showSnackBar('Unknown error: $e');
    }
  }

  void _showSnackBar(String msg) {
    _scaffoldKey.currentState.showSnackBar(new SnackBar(
      content: new Text(msg)
    ));
  }

  void _openWebviewVideo(String videoUrl, String videoTitle,
      String webviewUrl) {
    videoChannel.invokeMethod('openWebviewVideo', <String, String>{
      'videoUrl': videoUrl,
      'videoTitle': videoTitle,
      'webviewUrl': webviewUrl,
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              this.text,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _scanQrcode,
        tooltip: '扫描视频URL二维码',
        child: new Icon(Icons.camera_alt),
      ),
    );
  }
}
